

package com.hazelcast.query.impl.getters;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.internal.serialization.impl.DataInputNavigableJsonAdapter;
import com.hazelcast.internal.serialization.impl.NavigableJsonInputAdapter;

import java.io.IOException;

import static com.hazelcast.internal.serialization.impl.HeapData.HEAP_DATA_OVERHEAD;

public final class JsonDataGetter extends AbstractJsonGetter {

    private static final int UTF_CHARACTER_COUNT_FIELD_SIZE = 4;

    private final JsonFactory factory = new JsonFactory();

    private final InternalSerializationService ss;

    JsonDataGetter(InternalSerializationService ss) {
        super(null);
        this.ss = ss;
    }

    @Override
    protected JsonParser createParser(Object obj) throws IOException {
        Data data = (Data) obj;
        return factory.createParser(data.toByteArray(), HEAP_DATA_OVERHEAD + UTF_CHARACTER_COUNT_FIELD_SIZE, data.dataSize() - UTF_CHARACTER_COUNT_FIELD_SIZE);
    }

    @Override
    protected NavigableJsonInputAdapter annotate(Object object) {
        Data data = (Data) object;
        return new DataInputNavigableJsonAdapter(ss.createObjectDataInput(data), HEAP_DATA_OVERHEAD + UTF_CHARACTER_COUNT_FIELD_SIZE);
    }
}
